home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 March: Reference Library / Dev.CD Mar 96 RL / Dev.CD Mar 96 RL.toast / Technical Documentation / develop / develop Issue 25 / develop Issue 25 code / C++ exceptions in C / UException.h < prev   
Encoding:
Text File  |  1995-04-20  |  2.9 KB  |  116 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    UException.h                    ©1994 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //
  5. //    Exception Handling
  6. //
  7. //        CodeWarrior now supports "real" C++ Exception Handling, so the
  8. //        old macros map to the C++ syntax for exceptions.
  9. //
  10. //        However, it is still useful to use the Throw_() macro instead
  11. //        of "throw" for debugging purposes. If you #define Debug_Throw
  12. //        you enable the debugging options for monitoring throws.
  13. //        See the file "UDebugging.h" for details.
  14. //
  15. //    Exception Codes
  16. //
  17. //        The exception handling macros all used type ExceptionCode
  18. //        when throwing. An ExceptionCode is a signed 32-bit integer.
  19.  
  20. #pragma once
  21.  
  22. #include <UDebugging.h>
  23.  
  24. #ifndef __ERRORS__
  25. #include <Errors.h>
  26. #endif
  27.  
  28. #ifndef __MEMORY__
  29. #include <Memory.h>
  30. #endif
  31.  
  32. #ifndef __RESOURCES__
  33. #include <Resources.h>
  34. #endif
  35.  
  36.     // Exception codes
  37.  
  38. typedef long    ExceptionCode;
  39.  
  40. enum {
  41.     err_NilPointer        = 'nilP',
  42.     err_AssertFailed    = 'asrt'
  43. };
  44.  
  45.  
  46.     // Exception Handling Macros
  47.     //    These now map to the "real" Exception Handling syntax
  48.     
  49. #define Try_            try
  50. #define Catch_(err)        catch(ExceptionCode err)
  51. #define EndCatch_
  52. #define Throw(err)        throw (ExceptionCode)(err)
  53.  
  54.  
  55.     // Useful macros for signaling common failures
  56.     
  57.     
  58.         // This macro avoids evaluating "err" twice by assigning
  59.         // its value to a local variable.
  60.     
  61. #define ThrowIfOSErr_(err)                                            \
  62.     do {                                                            \
  63.         OSErr    __theErr = err;                                        \
  64.         if (__theErr != noErr) {                                    \
  65.             Throw_(__theErr);                                        \
  66.         }                                                            \
  67.     } while (false)
  68.  
  69. #define ThrowOSErr_(err)    Throw_(err)
  70.  
  71. #define    ThrowIfNil_(ptr)                                            \
  72.     do {                                                            \
  73.         if ((ptr) == nil) Throw_(err_NilPointer);                    \
  74.     } while (false)
  75.  
  76. #define    ThrowIfNULL_(ptr)                                            \
  77.     do {                                                            \
  78.         if ((ptr) == nil) Throw_(err_NilPointer);                    \
  79.     } while (false)
  80.  
  81. #define    ThrowIfResError_()    ThrowIfOSErr_(ResError())
  82. #define    ThrowIfMemError_()    ThrowIfOSErr_(MemError())
  83.  
  84. #define    ThrowIfResFail_(h)                                            \
  85.     do {                                                            \
  86.         if ((h) == nil) {                                            \
  87.             OSErr    __theErr = ResError();                            \
  88.             if (__theErr == noErr) {                                \
  89.                 __theErr = resNotFound;                                \
  90.             }                                                        \
  91.             Throw_(__theErr);                                        \
  92.         }                                                            \
  93.     } while (false)
  94.     
  95. #define    ThrowIfMemFail_(p)                                            \
  96.     do {                                                            \
  97.         if ((p) == nil) {                                            \
  98.             OSErr    __theErr = MemError();                            \
  99.             if (__theErr == noErr) __theErr = memFullErr;            \
  100.             Throw_(__theErr);                                        \
  101.         }                                                            \
  102.     } while (false)
  103.  
  104. #define    ThrowIf_(test)                                                \
  105.     do {                                                            \
  106.         if (test) Throw_(err_AssertFailed);                            \
  107.     } while (false)
  108.  
  109. #define    ThrowIfNot_(test)                                            \
  110.     do {                                                            \
  111.         if (!(test)) Throw_(err_AssertFailed);                        \
  112.     } while (false)
  113.  
  114. #define    FailOSErr_(err)        ThrowIfOSErr_(err)
  115. #define FailNIL_(ptr)        ThrowIfNil_(ptr)
  116.